package com.jlastu.znb.view;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.znb.R;
import com.jlastu.znb.adapter.NewsListAdapter;
import com.jlastu.znb.base.BaseFragment;
import com.jlastu.znb.contract.SchoolContract;
import com.jlastu.znb.entity.News;
import com.jlastu.znb.presenter.SchoolPresenter;

import java.util.ArrayList;
import java.util.List;

public class SchoolFragment extends BaseFragment implements SchoolContract.ISchoolView ,View.OnClickListener{

    private RecyclerView mNewsRecyclerView;
    private NewsListAdapter mRecyclerAdapter;
    private SchoolContract.ISchoolPresenter mSchoolPresenter;
    private List<News> mNewsList;
    private ImageView mSendNews;
    private ImageView mBack;
    private TextView mTextView1;
    private TextView mTextView2;
    private TextView mTextView3;
    private TextView mTextView4;
    private TextView mTextView5;
    private TextView mTextView6;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_school;
    }

    @Override
    protected void initView() {
        mSchoolPresenter = new SchoolPresenter(this, getContext());
        mNewsList = new ArrayList<>();
        mNewsRecyclerView = mView.findViewById(R.id.rv_news);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mNewsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new NewsListAdapter(mNewsList);
        mNewsRecyclerView.setAdapter(mRecyclerAdapter);

        mSendNews = mView.findViewById(R.id.send_news);
        mSendNews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), AddNewsActivity.class);
                startActivity(intent);
            }
        });

        mBack = mView.findViewById(R.id.school_back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), SecondActivity.class);
                startActivity(intent);
            }
        });


        mTextView1 = mView.findViewById(R.id.title1);
        mTextView1.setSelected(true);
        mTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 切换状态
                setTabSelected(mTextView1);
                mSchoolPresenter.getNewsByCategory("热点新闻");
            }
        });
        mTextView2 = mView.findViewById(R.id.title2);
        mTextView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView2);
                mSchoolPresenter.getNewsByCategory("专家咨询");
            }
        });
        mTextView3 = mView.findViewById(R.id.title3);
        mTextView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView3);
                mSchoolPresenter.getNewsByCategory("支农宝新闻");
            }
        });
        mTextView4 = mView.findViewById(R.id.title4);
        mTextView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView4);
                mSchoolPresenter.getNewsByCategory("植物病虫害");
            }
        });
        mTextView5 = mView.findViewById(R.id.title5);
        mTextView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView5);
                mSchoolPresenter.getNewsByCategory("农业百科");
            }
        });
        mTextView6 = mView.findViewById(R.id.title6);
        mTextView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView6);
                mSchoolPresenter.getNewsByCategory("气象预报");
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        // 假设你已经有了获取新闻列表的方法
        mSchoolPresenter.getNewsByCategory("热点新闻");

        // 添加 nong1.html 到热点新闻列表
        News nong1News = new News();
        nong1News.setTitle("巩固脱贫成果衔接乡村振兴政策研究成果交流会"); // 新闻标题
        nong1News.setDetailUrl("nong1.html"); // 设置 HTML 文件名
        // ... 设置其他属性 ...

        // 将 nong1.html 新闻添加到列表中
        mNewsList.add(0, nong1News); // 假设添加到列表顶部
        mRecyclerAdapter.updateData(mNewsList); // 更新 RecyclerView 数据
    }

    @Override
    protected void initListener() {


    }

    private void setTabSelected(TextView selectedTab) {
        // 重置所有标签为未选中状态
        mTextView1.setSelected(false);
        mTextView2.setSelected(false);
        mTextView3.setSelected(false);
        mTextView4.setSelected(false);
        mTextView5.setSelected(false);
        mTextView6.setSelected(false);
        // 将被点击的标签设置为选中状态
        selectedTab.setSelected(true);
    }


    @Override
    public void showNewsByCategory(List<News> newsList) {
        mNewsList =newsList;
        Log.d(TAG, "showNewsByCategory: "+newsList);
        mRecyclerAdapter.updateData(newsList); // 刷新RecyclerView数据
    }

    @Override
    public void onClick(View v) {

    }
}
